package com.im.project.service.impl;

import com.im.common.component.ThreadPoolExecutorHandler;
import com.im.common.config.MinioConfig;
import com.im.common.constant.Constant;
import com.im.common.enums.ResultEnum;
import com.im.common.exception.BusinessException;
import com.im.project.service.MinioFileService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * minio文件 服务层接口实现类
 *
 * @author yunZhouJiaDi
 */
@Slf4j
@Service
public class MinioFileServiceImpl implements MinioFileService {

    @Resource
    private MinioClient minioClient;

    @Resource
    private MinioConfig minioConfig;

    @Override
    public String upload(MultipartFile file) {
        if (file.getSize() > Constant.SystemConstant.MAX_FILE_SIZE) {
            throw new BusinessException(ResultEnum.MAX_FILE_SIZE_ERROR.getCode(), ResultEnum.MAX_FILE_SIZE_ERROR.getMessage());
        }
        String fileName = file.getOriginalFilename();
        uploadCommon(file, fileName);
        log.info("单上传成功访问地址为: {}", fileName);
        return fileName;
    }

    @Override
    public List<String> uploadFiles(MultipartFile[] files) {
        if (files.length > 6) {
            throw new BusinessException(ResultEnum.FILE_CANNOT_EXCEED_SIX.getCode(), ResultEnum.FILE_CANNOT_EXCEED_SIX.getMessage());
        }
        long totalSize = 0;
        for (MultipartFile file : files) {
            totalSize += file.getSize();
        }
        if (totalSize > Constant.SystemConstant.MAX_FILE_SIZE) {
            throw new BusinessException(ResultEnum.MAX_FILE_SIZE_ERROR.getCode(), ResultEnum.MAX_FILE_SIZE_ERROR.getMessage());
        }
        List<String> list = new ArrayList<String>();
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            list.add(fileName);
            uploadCommon(file, fileName);
        }
        log.info("多上传成功访问地址为: {}", list);
        return list;
    }

    private Boolean uploadCommon(MultipartFile file, String fileName) {
        ThreadPoolExecutorHandler.threadPoolExecutor.execute(() -> {
            try {
                InputStream inputStream = file.getInputStream();
                minioClient.putObject(PutObjectArgs.builder()
                        .bucket(minioConfig.getBucketName())
                        .object(fileName)
                        .stream(inputStream, file.getSize(), -1)
                        .contentType(file.getContentType())
                        .build());
                inputStream.close();
            } catch (ErrorResponseException e) {
                e.printStackTrace();
            } catch (InsufficientDataException e) {
                e.printStackTrace();
            } catch (InternalException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (InvalidResponseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (XmlParserException e) {
                e.printStackTrace();
            }
        });
        return true;
    }

}
